home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / AutoBin Source / DSAppleEvents.c < prev    next >
Text File  |  1992-06-19  |  8KB  |  252 lines

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DSAppleEvents.c
  5. **
  6. **   Description:    Generic AppleEvent handling routines
  7. **                    
  8. **                    This is the set of routines for handling the required Apple events.
  9. **                    You should NEVER have to modify this file!!!
  10. **                    Simply add code in DSUserProcs to the routines called by these.
  11. **
  12. *******************************************************************************
  13. **                       A U T H O R   I D E N T I T Y
  14. *******************************************************************************
  15. **
  16. **    Initials    Name
  17. **    --------    -----------------------------------------------
  18. **    LDR            Leonard Rosenthol
  19. **    MTC            Marshall Clow
  20. **    SCS            Stephan Somogyi
  21. **
  22. *******************************************************************************
  23. **                      R E V I S I O N   H I S T O R Y
  24. *******************************************************************************
  25. **
  26. **      Date        Time    Author    Description
  27. **    --------    -----    ------    ---------------------------------------------
  28. **    11/24/91            LDR        Added a handler for 'pdoc' as per DTS recommendation
  29. **                                    This caused some reorg & userProc routine changes
  30. **                                    I also created a new common AEVT doc extractor
  31. **                                Cleaned up error handling by adding FailErr
  32. **                                Cleaned up the placement of braces
  33. **                                Added the passing of a userDataHandle to the odoc/pdoc routines
  34. **    10/29/91            SCS        Changes for THINK C 5
  35. **    10/28/91            LDR        Officially renamed DropShell (from QuickShell)
  36. **                                Added a bunch of comments for clarification
  37. **    10/06/91    00:02    MTC        Converted to MPW C
  38. **    04/09/91    00:02    LDR        Added to Projector
  39. **
  40. ******************************************************************************/
  41.  
  42. #include "DSGlobals.h"
  43. #include "DSUserProcs.h"
  44. #include "DSAppleEvents.h"
  45.  
  46.  
  47. /*
  48.     This routine does all initialization for AEM, including the
  49.     creation and then population of the dispatch table.
  50. */
  51. #pragma segment Initialize
  52. pascal void InitAEVTStuff ()  {
  53.     OSErr aevtErr;
  54.  
  55.     aevtErr = noErr;
  56.     
  57.     if ( aevtErr == noErr )
  58.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEOpenApplication,
  59.                      (EventHandlerProcPtr) HandleOAPP, 0, false );
  60.  
  61.     if ( aevtErr == noErr )
  62.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEOpenDocuments,
  63.                     (EventHandlerProcPtr) HandleODOC, 0, false );
  64.  
  65.     if ( aevtErr == noErr )
  66.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEPrintDocuments,
  67.                     (EventHandlerProcPtr) HandlePDOC, 0, false );
  68.  
  69.     if ( aevtErr == noErr )
  70.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEQuitApplication,
  71.                     (EventHandlerProcPtr) HandleQuit, 0, false );
  72.  
  73.     if ( aevtErr == noErr )
  74.         InstallOtherEvents ();
  75.         
  76.     if ( aevtErr != noErr )
  77.         ;        // report an error if you are so included
  78. }
  79.  
  80.  
  81.  
  82. /*    
  83.     This routine is a utility routine for checking that all required 
  84.     parameters in the Apple event have been used.
  85. */
  86. #pragma segment Main
  87. OSErr GotRequiredParams ( AppleEvent *theAppleEvent ) {
  88.     DescType    typeCode;
  89.     Size        actualSize;
  90.     OSErr        retErr, err;
  91.  
  92.     err = AEGetAttributePtr ( theAppleEvent, keyMissedKeywordAttr,
  93.                     typeWildCard, &typeCode, NULL, 0, &actualSize );
  94.     
  95.     if ( err == errAEDescNotFound )    // we got all the required params: all is ok
  96.         retErr = noErr;
  97.     else if ( err == noErr )
  98.         retErr = errAEEventNotHandled;
  99.     else 
  100.         retErr = err;
  101.     
  102.     return retErr;
  103. }
  104.  
  105. /*
  106.     This is another routine useful for showing debugging info.
  107.     It calls the ErrorAlert routine from DSUtils to put up the 
  108.     error message.
  109.  
  110. */
  111. void FailErr(OSErr err) {
  112.  
  113.     if (err != noErr)
  114.         ErrorAlert(kErrStringID, kAEVTErr, err);
  115. }
  116.  
  117. /*    
  118.     This routine is the handler for the oapp (Open Application) event.
  119.     
  120.     It first checks the number of parameters to make sure we got them all 
  121.     (even though we don't want any) and then calls the OpenApp userProc in QSUserProcs.
  122.     Finally it checks to see if the caller wanted a reply & sends one, setting any error.
  123. */
  124. #pragma segment Main
  125. pascal OSErr HandleOAPP ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon ) {
  126. #pragma unused ( handlerRefcon )
  127.     OSErr err;
  128.  
  129.     FailErr(err = GotRequiredParams ( theAppleEvent ));
  130.  
  131.     // let's show the user the splash screen
  132.     ShowWindow(gSplashScreen);
  133.  
  134.     OpenApp ();        // pass it on to the app specific routine
  135.  
  136.     if ( reply->dataHandle != NULL )    /*    a reply is sought */
  137.         FailErr(err = AEPutParamPtr ( reply, 'errs', 'TEXT', "Opening", 7 ));
  138.     
  139.     return err;
  140. }
  141.  
  142.  
  143. /*    
  144.     This routine is the handler for the quit (Quit Application) event.
  145.     
  146.     It first checks the number of parameters to make sure we got them all 
  147.     (even though we don't want any) and then calls the QuitApp userProc in QSUserProcs.
  148.     Finally it checks to see if the caller wanted a reply & sends one, setting any error.
  149. */
  150.  
  151. #pragma segment Main
  152. pascal OSErr HandleQuit ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon ) {
  153. #pragma unused ( handlerRefcon )
  154.     OSErr err;
  155.     
  156.     FailErr( err = GotRequiredParams ( theAppleEvent ));
  157.  
  158.     QuitApp ();        // pass it on to the app specific routine
  159.  
  160.     if ( reply->dataHandle != NULL )    /*    a reply is sought */
  161.         FailErr(err = AEPutParamPtr ( reply, 'errs', 'TEXT', "Qutting", 7 ));
  162.     
  163.     return err;
  164. }
  165.  
  166.  
  167. /*    
  168.     This routine is the low level processing routine for both the 
  169.     odoc (Open Document) and pdoc (Print Document) events.
  170.     
  171.     This routine is the key one, since this is how we get the list of
  172.     files/folders/disks to process.  The first thing to do is the get the
  173.     list of files, and then make sure that's all the parameters (should be!).
  174.     We then send call the PreflightDocs routine (from DSUserProcs), process
  175.     each file in the list by calling OpenDoc (again in DSUserProcs), and finally
  176.     call PostflightDocs (you know where) and setting a return value.
  177. */
  178. #pragma segment Main
  179. pascal OSErr _HandleDocs ( AppleEvent *theAppleEvent, AppleEvent *reply, Boolean opening ) {
  180. #pragma unused ( reply )
  181. #pragma unused ( handlerRefcon )
  182.     OSErr        err;
  183.     FSSpec        myFSS;
  184.     AEDescList    docList;
  185.     long        index, itemsInList;
  186.     Size        actualSize;
  187.     AEKeyword    keywd;
  188.     DescType    typeCode;
  189.     Handle        userDataHandle;
  190.     
  191.  
  192.     FailErr(err = AEGetParamDesc ( theAppleEvent, keyDirectObject, typeAEList, &docList ));
  193.     FailErr(err = GotRequiredParams ( theAppleEvent ));
  194.  
  195.     if (PreFlightDocs (opening, &userDataHandle))    {    // let the app do any preflighting it might need
  196.  
  197.         /*    How many items do we have?. */
  198.         FailErr(err = AECountItems ( &docList, &itemsInList ));
  199.     
  200.         for ( index = 1; index <= itemsInList; index++ ) {
  201.             FailErr(err = AEGetNthPtr ( &docList, index, typeFSS, &keywd, &typeCode,
  202.                     (Ptr) &myFSS, sizeof ( myFSS ), &actualSize ));
  203.     
  204.             OpenDoc( &myFSS, opening, userDataHandle );    // call the userProc
  205.         }
  206.     
  207.         PostFlightDocs (opening, userDataHandle);    // cleanup time
  208.     }
  209.     else
  210.         err = errAEEventNotHandled;    // tells AEM that we didn't handle it!
  211.         
  212.     FailErr(AEDisposeDesc ( &docList ));
  213.  
  214.     return err;
  215. }
  216.  
  217. /*
  218.     This routine is the handler for the odoc (Open Document) event.
  219.     
  220.     The odoc event simply calls the common _HandleDocs routines, which will
  221.     do the dirty work of parsing the AEVT & calling the userProcs.
  222. */
  223. #pragma segment Main
  224. pascal OSErr HandleODOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon ) {
  225. #pragma unused ( handlerRefcon )
  226.     
  227.     return (_HandleDocs(theAppleEvent, reply, true));    // call the low level routine
  228. }
  229.  
  230. /*
  231.     This routine is the handler for the pdoc (Print Document) event.
  232.     
  233.     The pdoc event like the odoc simply calls the common _HandleDocs routines
  234. */
  235. #pragma segment Main
  236. pascal OSErr HandlePDOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon ) {
  237. #pragma unused ( handlerRefcon )
  238.     
  239.     return (_HandleDocs(theAppleEvent, reply, false));    // call the low level routine
  240. }
  241.  
  242. /*    
  243.     This is the routine called by the main event loop, when a high level
  244.     event is found.  Since we only deal with Apple events, and not other
  245.     high level events, we just pass everything onto the AEM via AEProcessAppleEvent
  246. */
  247. #pragma segment Main
  248. pascal void DoHighLevelEvent ( EventRecord *event ) {
  249.  
  250.     FailErr ( AEProcessAppleEvent ( event ) );
  251. }
  252.